package com.example.asynctaskdemo;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class LessonsAdapter extends BaseAdapter{
	
	private Context context;
	private LayoutInflater inflater;
	private List<LessonBean> datas;
	private ImageLoader mImageLoader;
	
	public LessonsAdapter(Context context, List<LessonBean> datas) {
		this.context = context;
		if (datas != null) {
			this.datas = datas;
		}
		inflater = LayoutInflater.from(context);
		mImageLoader = new ImageLoader();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return datas.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return datas.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_layout, null);
			holder = new ViewHolder();
			holder.title = (TextView) convertView.findViewById(R.id.title);
			holder.content = (TextView) convertView.findViewById(R.id.content);
			holder.icon = (ImageView) convertView.findViewById(R.id.lesson_icon);
			convertView.setTag(holder);
		}else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.title.setText(datas.get(position).getTitle());
		holder.content.setText(datas.get(position).getContent());
		holder.icon.setImageResource(R.drawable.ic_empty);
		String url = datas.get(position).getIcon();
		holder.icon.setTag(url);
//		new ImageLoader().showImageByThread(holder.icon, url);
		mImageLoader.showImageByAsyncTask(holder.icon, url);
		return convertView;
	}
	
	class ViewHolder{
		private TextView title, content;
		private ImageView icon;
	}

}
